package com.doge.service.wx.footprint;

import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.pojo.footprint.Footprint;
import com.doge.bean.pojo.footprint.FootprintExample;
import com.doge.bean.pojo.goods.Goods;
import com.doge.bean.pojo.goods.GoodsExample;
import com.doge.bean.vo.goods.GoodsFootprintVO;
import com.doge.mapper.FootprintMapper;
import com.doge.mapper.GoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WxFootprintServiceImpl implements WxFootprintService {
    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public BaseData list(BaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());
        FootprintExample example = new FootprintExample();
        FootprintExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Footprint> footprints = footprintMapper.selectByExample(example);
        List<Integer> goodsIdList = new ArrayList<>();
        for (Footprint footprint : footprints) {
            Integer goodsId = footprint.getGoodsId();
            goodsIdList.add(goodsId);
        }
        //现在有了一堆goodsId了，查询goods商品的详细信息，返回给前端
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria goodsCriteria = goodsExample.createCriteria();
        goodsCriteria.andIdIn(goodsIdList);
        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
        List<GoodsFootprintVO> footPrintList = new ArrayList<>();

        for (Goods goods : goodsList) {
            for (Footprint footprint : footprints) {
                GoodsFootprintVO vo = new GoodsFootprintVO();
                vo.setBrief(goods.getBrief());
                vo.setPicUrl(goods.getPicUrl());
                vo.setAddTime(footprint.getAddTime());
                vo.setGoodsId(goods.getId());
                vo.setName(goods.getName());
                vo.setId(footprint.getId());
                vo.setRetailPrice(goods.getRetailPrice());
                footPrintList.add(vo);
            }
        }

        PageInfo pageInfo = new PageInfo(footPrintList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        BaseData data = BaseData.list(footPrintList, total);
        data.setLimit(baseParam.getLimit());
        data.setPage(baseParam.getPage());
        data.setPages(pages);
        return data;
    }

    @Override
    public int delete(Integer id) {
        FootprintExample example = new FootprintExample();
        FootprintExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        Footprint footprint = footprintMapper.selectByPrimaryKey(id);
        footprint.setDeleted(true);
        int rows = footprintMapper.updateByPrimaryKey(footprint);
        return rows;
    }
}